package dao;

import dao.base.BaseDao;
import model.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import utils.JDBCUtil;

import java.util.List;
import java.util.Map;


public class UserDao extends BaseDao<User> {

    private JdbcTemplate jdbcTemplate;

    public UserDao(Class aClass) {
        super(User.class);
    }

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    public User findByName(String username){
        User user;
        try {
            jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
            user = jdbcTemplate.queryForObject("select * from user where username = ?"
                    , new BeanPropertyRowMapper<>(User.class), username);
        } catch (Exception e) {
            return null;
        }
        return user;
    }

    /**
     * 分页查询用户
     * @param startIndex
     * @param pageSize
     * @return
     */
    public List<User> search(Integer startIndex, Integer pageSize, String realName) {
        String sql = "select * from user where real_name like ? limit ?,?";

        try {
            jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),"%"+realName+"%",startIndex,pageSize);
        } catch (Exception e) {
            return null;
        }
    }

    public List<User> fuzzySearch(String realName) {
        String sql = "select * from user where real_name like ?";
        try {
            jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),"%"+realName+"%");
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 用户个人基本信息
     * @param id
     * @return
     */
    public Map<String , Object> findBaseInfo(Integer id){
        Map<String , Object> map;
        String sql = "select u.id as uId,u.real_name as realName,u.username,d.id as deptId,d.`name` as deptName " +
                "from user u " +
                "left join dept d " +
                "on d.id = u.dept_id " +
                "where u.id = ?";
        try {
            jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
            map = jdbcTemplate.queryForMap(sql, id);
        } catch (Exception e) {
            return null;
        }
        return map;
    }

    /**
     * 查询用户所有信息
     * @param id
     * @return
     */
    public Map<String , Object> findAllInfo(Integer id){
        Map<String , Object> map;
        String sql = "select u.*,d.`name` as deptName," +
                "(select count(1) from userfocus where user_focus_id = ?) as totalFocus " +
                "from user u " +
                "left join dept d " +
                "on d.id = u.dept_id " +
                "where u.id = ?";
        try {
            jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
            map = jdbcTemplate.queryForMap(sql, id, id);
            return map;
        } catch (Exception e) {
            return null;
        }
    }

    public List<Integer> findFocus(Integer id){
        try{
            jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
            return jdbcTemplate.query(
                    "select user_focus_id from userfocus where user_id=?", new SingleColumnRowMapper<>(Integer.class),id);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 查询关注记录
     * @param id
     * @param userId
     * @return
     */
    public Map<String, Object> findFocusByUserIdAndFocusId(Integer id, Integer userId){
        try{
            jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
            return jdbcTemplate.queryForMap(
                    "select * from userfocus where user_id=? and user_focus_id=?",id,userId);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 添加用户关注
     * @param id
     * @param userId
     */
    public void insertFocus(Integer id, Integer userId){
        jdbcTemplate = getJdbcTemplate();
        jdbcTemplate.update("insert into userfocus values(?,?)", id, userId);
    }

    /**
     * 取关
     * @param id
     * @param userId
     */
    public void unFocus(Integer id, Integer userId) {
        jdbcTemplate = getJdbcTemplate();
        jdbcTemplate.update("delete from userfocus where user_id=? and user_focus_id=?", id, userId);
    }

    /**
     * 被查看自增
     * @param id
     */
    public void incLook(Integer id) {
        jdbcTemplate = getJdbcTemplate();
        jdbcTemplate.update("update user set look=look+1 where id=?", id);
    }

    /**
     * 用户关注数
     * @param userId
     * @return
     */
    public Integer countByUserId(Integer userId){
        try{
            jdbcTemplate = getJdbcTemplate();
            return jdbcTemplate.queryForObject(
                    "select count(1) from userfocus where user_id=?", Integer.class, userId);
        }catch (Exception e){
            return 0;
        }
    }

    public List<Map<String, Object>> findFocusPage(Integer userId, Integer startIndex, Integer pageSize) {
        String sql = "select u.id as userId,u.username as username ,u.real_name as realName,u.is_secret as isSecret from user u where " +
                "u.id in (select uf.user_focus_id userId " +
                "from userfocus uf " +
                "left join user u " +
                "on u.id = uf.user_id " +
                "where uf.user_id = ?) limit ?,?";
        jdbcTemplate = getJdbcTemplate();
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,userId,startIndex,pageSize);
        return maps;
    }

    public User findByWxOpenid(String openid) {
        return getJdbcTemplate().queryForObject("select * from user where wx_openid = ?",User.class,openid);
    }
}
